﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Actya.Web.Infrastructure.ElFinderConnector.Utils
{
    public class CacheUtils
    {
        private const string cFinderPrefix = "elFinder_";

        public static string GetPathByHash(string hash)
        {
            if (Configuration.Configuration.CacheDuration == 0)
                return null;

            var key = cFinderPrefix + hash;

            var cache = System.Web.HttpContext.Current.Cache;
            return cache.Get(key) != null
                ? cache.Get(key).ToString()
                : null;
        }

        public static void AddOrUpdateHashPath(string hash, string path)
        {
            if (Configuration.Configuration.CacheDuration == 0)
                return;

            var key = cFinderPrefix + hash;

            var cache = System.Web.HttpContext.Current.Cache;

            if (cache.Get(key) != null)
            {
                cache[key] = path;
            }
            else
            {
                cache.Add(key, path, null, DateTime.Now.AddMinutes(Configuration.Configuration.CacheDuration), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);                    
            }
            
        }

        public static void RemoveHashPath(string hash)
        {
            if (Configuration.Configuration.CacheDuration == 0)
                return;

            var key = cFinderPrefix + hash;

            var cache = System.Web.HttpContext.Current.Cache;

            cache.Remove(key);
        }
    }
}
